package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.TestRankBean;
import cn.bocweb.spinning.view.common.GlideCircle;

/**
 * Created by chenchangxing on 15/12/23.
 */
public class RankAdapter extends RecyclerView.Adapter {

    List<TestRankBean.DataEntity> list;
    Context context;


    public RankAdapter(List<TestRankBean.DataEntity> list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rank, null);
        ViewHolder holder = new ViewHolder(view);
        context = parent.getContext();
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder myHolder = (ViewHolder) holder;
        myHolder.rankItemPosition.setText(String.valueOf(position + 1));
//        if (list.get(position).getGender().equals("1")) {
//            myHolder.rankItemGender.setImageResource(R.mipmap.rank_male);
//        } else if (list.get(position).getGender().equals("2")) {
//            myHolder.rankItemGender.setImageResource(R.mipmap.rank_female);
//        } else {
//            myHolder.rankItemGender.setVisibility(View.GONE);
//        }
        Glide
                .with(context)
                .load(list.get(position).getUthumb())
                .placeholder(R.mipmap.drawer_icon_unlogin)
                .transform(new GlideCircle(context))
                .into(myHolder.rankItemIcon);
        myHolder.rankItemName.setText(list.get(position).getNickname());
        myHolder.rankItemTimeScore.setText(list.get(position).getScore() + "分");
        String timeSpend = Integer.parseInt(list.get(position).getSpend_time()) / 60 + "分" + Integer.parseInt(list.get(position).getSpend_time()) % 60 + "秒";
        myHolder.rankItemTimeSpend.setText(timeSpend);
        double rank = list.get(position).getRank();
        double total = Double.parseDouble(list.get(position).getTotal());
        double percent;
        if (Double.compare(rank, total) == 0 && total == 1) {
            percent = 100;
        } else {
            percent = (total - rank) * 100 / (total  - 1);
        }

        DecimalFormat df = new DecimalFormat("######0");

        myHolder.rankItemPercent.setText(String.valueOf(df.format(percent)) + "%");
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.rank_item_position)
        TextView rankItemPosition;
        @Bind(R.id.rank_item_icon)
        ImageView rankItemIcon;
        @Bind(R.id.rank_item_name)
        TextView rankItemName;
        @Bind(R.id.rank_item_gender)
        ImageView rankItemGender;
        @Bind(R.id.rank_item_time_spend)
        TextView rankItemTimeSpend;
        @Bind(R.id.rank_item_percent)
        TextView rankItemPercent;
        @Bind(R.id.rank_item_time_score)
        TextView rankItemTimeScore;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }
}
